/*Author:no-bald-chen
* Time:2021/8/12
* */

package utils.tree;

public class BinaryTree implements Tree{
    private TreeNode root;
    private int[] ints;
    private int k;

    public BinaryTree(int[] ints){
        this.ints = ints;
        k=0;
    }

    public BinaryTree() {
    }

    @Override
    public TreeNode getRoot() {
        return root;
    }

    @Override
    public TreeNode InitTree() {
        int i = ints[k++];
        if (i==0){
            return null;
        }
        TreeNode treeNode = new TreeNode(i,null,null);

        treeNode.left =InitTree();
        treeNode.right =InitTree();
        return treeNode;
    }

    @Override
    public  void PreorderTravel(TreeNode treeNode) {
        if (treeNode==null){
            return;
        }

        System.out.print(treeNode.val+" ");
        PreorderTravel(treeNode.left);
        PreorderTravel(treeNode.right);
    }

    @Override
    public void InorderTravel(TreeNode treeNode) {
        if (treeNode==null){
            return;
        }
        InorderTravel(treeNode.left);
        System.out.print(treeNode.val+" ");
        InorderTravel(treeNode.right);
    }

    @Override
    public void PostorderTravel(TreeNode treeNode) {
        if (treeNode==null){
            return;
        }
        PostorderTravel(treeNode.left);
        PostorderTravel(treeNode.right);
        System.out.print(treeNode.val+" ");
    }
}
